﻿using System;
using System.Collections.Generic;
using System.Linq;
using Medianamik.Core;
using System.Web.UI.WebControls;
using Medianamik.Core.Sugar;

namespace Medianamik.UI.Controls
{
    public class DropDownList : BaseMedianamikControl
    {
        private readonly System.Web.UI.WebControls.DropDownList _ddList = new System.Web.UI.WebControls.DropDownList()
            {
                ID = ControlToValidateId,
                CssClass = "SelectList"
            };

        public BasePage BasePage
        {
            get
            {
                return (BasePage)base.Page;
            }
        }

        public override object GetValue()
        {

            Guid g;
            return _ddList.Items.Cast<ListItem>()
                .Where(i => i.Selected && StringHelper.IsGuid(i.Value, out g))
                .Select(i => (Guid?)new Guid(i.Value))
                .FirstOrDefault();

        }

        public override void SetValue(object value)
        {
            if (value != null)
                _ddList.SelectedValue = value.ToString();
            else
                _ddList.SelectedValue = string.Empty;
        }

        public override Type TypeOfValue
        {
            get { return typeof(Guid?); }
        }

        protected override void CreateControlHierarchy(bool pUseDataSource)
        {
            Controls.Add(_ddList);

            if (pUseDataSource)
            {
                _ddList.Items.Insert(0, string.Empty);

                if (Options.Count > 0)
                {
                    //La propriété est normalement identique pour toutes les options.
                    var propertyId = Options[0].PropertyId;
                    var optionNodes = GetOptionNodes(Options.Select(o => o.NodeId).ToArray());

                    OrderItems(ref optionNodes);
                    _ddList.Items.AddRange(BuildItems(optionNodes, propertyId));


                    AddOtherItem(_ddList);
                }

                var selectedValue = (Guid?)DataSource;
                if (selectedValue.HasValue)
                {
                    var item = _ddList.Items.FindByValue(selectedValue.Value.ToString());
                    if (item != null) { item.Selected = true; }
                }
            }
        }

        protected virtual ListItem[] BuildItems(IList<Node> optionNodes, Guid propertyId)
        {
            return optionNodes.Select(n => new ListItem(n.DefaultInstance[propertyId].GetValue<string>(), n.ID.ToString()))
                .ToArray();
        }

        protected virtual void AddOtherItem(System.Web.UI.WebControls.DropDownList list)
        {

        }

        protected virtual IList<Node> GetOptionNodes(IEnumerable<Guid> options)
        {
            return NodeManager.GetNodes(options);
        }

        protected virtual void OrderItems(ref IList<Node> items)
        {

        }
    }
}
